<?php

/**
 * 递归删除链表指定元素
 * Class LinkedListRecursion
 */
class LinkedListRecursion
{
    public static function deleteElement(LinkedList $linkedList, $val)
    {
        $linkedList->setHead(self::recursionDelete($linkedList->getHead(), $val));
        return $linkedList;
    }

    /**
     * 递归函数 递归删除链表元素
     * @param $head
     * @param $val
     * @return null
     */
    private static function recursionDelete($head, $val)
    {
        if ($head == null) {
            return null;
        } else {
            if ($head->e == $val) {
                return self::recursionDelete($head->next, $val);
            } else {
                $head->next = self::recursionDelete($head->next, $val);
                return $head;
            }
        }
    }
}